﻿using System.Collections.Generic;
using System.Linq;

namespace Sidvall.Data
{
    public class KeyValueCollection<TKey, TValue> : System.Collections.ObjectModel.Collection<KeyValue<TKey, TValue>>
    {
        #region Public Members


        #region Add

        public void Add(TKey key, TValue value)
        {
            KeyValue<TKey, TValue> item;

            item = new KeyValue<TKey, TValue>()
            {
                Key = key,
                Value = value,
            };
            Add(item);
        }

        #endregion
        #region CopyByKey

        public KeyValueCollection<TKey, TValue> CopyByKey(IEnumerable<TKey> includedKeys)
        {
            if (includedKeys == null)
                return new KeyValueCollection<TKey, TValue>();
            var index = new HashSet<TKey>(includedKeys);
            var items = new KeyValueCollection<TKey, TValue>();
            foreach (var item in this)
            {
                if (index.Contains(item.Key))
                    items.Add(item);
            }
            return items;
        }

        #endregion
        #region CopyByValue

        public KeyValueCollection<TKey, TValue> CopyByValue(IEnumerable<TValue> includedValues)
        {
            if (includedValues == null)
                return new KeyValueCollection<TKey, TValue>();
            var index = new HashSet<TValue>(includedValues);
            var items = new KeyValueCollection<TKey, TValue>();
            foreach (var item in this)
            {
                if (index.Contains(item.Value))
                    items.Add(item);
            }
            return items;
        }

        #endregion
        #region FindByKey

        public KeyValue<TKey, TValue> FindByKey(TKey key)
        {
            return (from o in this
                    where o.Key.Equals(key)
                    select o).FirstOrDefault();
        }

        #endregion
        #region GetKeys

        public TKey[] GetKeys()
        {
            return (from o in this
                    select o.Key).ToArray();
        }

        #endregion
        #region GetValues

        public TValue[] GetValues()
        {
            return (from o in this
                    select o.Value).ToArray();
        }

        #endregion
        #region RemoveByKey

        public void RemoveByKey(TKey key)
        {
            var item = (from o in this
                        where o.Key.Equals(key)
                        select o).FirstOrDefault();
            if (item != null)
                Remove(item);
        }

        #endregion
        #region RemoveByValue

        public void RemoveByValue(TValue value)
        {
            var item = (from o in this
                        where o.Value.Equals(value)
                        select o).FirstOrDefault();
            if (item != null)
                Remove(item);
        }

        #endregion

        #endregion
        #region Constructors

        public KeyValueCollection()
        {
        }

        #endregion
    }
}
